package com.eliansun.service.mongo.impl;

import com.eliansun.service.mongo.BaseDao;
import com.eliansun.service.mongo.IMongoWapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.mongodb.client.FindIterable;
import com.mongodb.client.result.UpdateResult;
import org.apache.log4j.Logger;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by xpq on 2017/3/22.
 */
@Component("mongoWapper")
public class MongoWapperImpl implements IMongoWapper {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private BaseDao baseDAO;

    @Override
    public FindIterable<Document> selectByCondition(Document document) {
        FindIterable<Document> findIterable = null;
        try {
            findIterable =  baseDAO.getConnection().find(document);
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:selectByCondition()异常:" + e.getMessage());
            return null;
        }
        return findIterable;
    }

    @Override
    public Document selectOneAndUpdate(Document document1,Document document2) {
        Document res = null;
        try {
            res = baseDAO.getConnection().findOneAndUpdate(document1,document2);
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:selectOneAndUpdate()异常:" + e.getMessage());
            return null;
        }
        return res;
    }

    @Override
    public Document selectOneAndDelete(Document document) {
        Document res = null;
        try {
            res = baseDAO.getConnection().findOneAndDelete(document);
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:selectOneAndDelete()异常:" + e.getMessage());
            return null;
        }
        return res;
    }


    @Override
    public void clearAllList() {
        try {
            baseDAO.getConnection().drop();
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:clearAllList()异常:" + e.getMessage());
        }
    }

    @Override
    public long size() {
        long count = 0;
        try {
            count = baseDAO.getConnection().count();
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:size()异常:" + e.getMessage());
        }
        return count;
    }

    @Override
    public void save(Document document) {
        try {
            baseDAO.getConnection().insertOne(document);
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:save()异常:" + e.getMessage());
        }
    }

    @Override
    public void closeConnection() {
        baseDAO.closeClient();
    }

    @Override
    public void setCollectionName(String collectionName) {
        baseDAO.setCollectionName(collectionName);
    }

    @Override
    public Document selectAndReplace(Document var1, Document var2) {
        Document document;
        try {
            document = baseDAO.getConnection().findOneAndReplace(var1,var2);
            return document;
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:saveAndReplace()异常:" + e.getMessage());
        }
        return null;
    }

    @Override
    public UpdateResult updateOne(Document var1, Document var2) {
        UpdateResult updateResult;
        try {
            updateResult = baseDAO.getConnection().updateOne(var1,var2);
            return updateResult;
        } catch (Exception e) {
            logger.error(getClass().getName() + "->method:saveAndReplace()异常:" + e.getMessage());
        }
        return null;
    }
}
